<?php
/***
*
* Class: WechatValidator //微信验证器
* Author: Jason ZHENG
* Data: 2018-07-25
*
***/
namespace App\Validator;

use App\Service\DataReturnService as DRS;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Validator;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Session; 
use App\Common\Base;

class WechatValidator
{
	public static function wexinPayOrderValidate($order, $totalFee)
	{
		if($order->total_price < $totalFee){
            return DRS::returnError(1002,'订单金额不对应!');
        }elseif($order->payment_status !== 0 && $order->payment_status !== null){
            if($order->payment_status == 1){
                return DRS::returnError(1002,'该订单已支付过，无需重新操作!');
            }elseif($order->payment_status == 2){
                return DRS::returnError(1002,'该订单已申请退款!');
            }elseif($order->payment_status == 3){
                return DRS::returnError(1002,'该订单已退款!');
            }else{
                return DRS::returnError(1002,'该订单支付状态出错!');
            }
        }elseif ($order->status !== 1) {
            return DRS::returnError(1002,'该订单状态不在支付状态!');
        }elseif ($order->delivery_id == null) {
            return DRS::returnError(1002,'该订单未添加收货人地址!');
        }else{
        	$delivery = DB::table('delivery')
                ->where([
                            'id' 		=> $order->delivery_id, 
                            'user_id' 	=> $order->user_id, 
                            'state' 	=> 0
                        ])
                ->first();
            if($delivery == null){
            	return DRS::returnError(1003,'该收货地址不存在！');
            }elseif($delivery->province == null || 
            		$delivery->city == null || 
            		$delivery->district == null ||
            	    $delivery->address == null ||
            		$delivery->phone == null ||
            	    $delivery->name == null)
            {
            	return DRS::returnError(1003,'该收货地址信息不完整！');
            }else{
            	return DRS::returnOk((array)$order);
            }
        }
	}

	public static function wexinPayUnitOrderValidate($batchcode, $userId, $totalFee)
	{
		$unitOrderQuery = DB::table('unit_order')
                        ->where([
                                    'pay_batchcode' => $batchcode, 
                                    'user_id' 		=> $userId, 
                                    'state' 		=> 0
                                ])
                        ->first();
        if($unitOrderQuery == null){
            return DRS::returnError(1001,'不存在该分笔子订单!');
        }elseif($unitOrderQuery->price != $totalFee){
            return DRS::returnError(1001,'支付金额与分笔子订单金额不匹配!');
        }elseif($unitOrderQuery->payment_status == 1){
            return DRS::returnError(1001,'该分笔子订单已支付!');
        }else{
        	$order = DB::table('orders')
                        ->where([
                                    'batchcode' => $unitOrderQuery->batchcode, 
                                    'user_id' 	=> $userId, 
                                    'state' 	=> 0
                                ])
                        ->first();
            if($order == null){
            	return DRS::returnError(1002,'不存在该订单!');
            }elseif($order->payment_status !== 0 && $order->payment_status !== null){
                    if($order->payment_status == 1){
                        return DRS::returnError(1002,'该订单已支付过，无需重新操作!');
                    }elseif($order->payment_status == 2){
                        return DRS::returnError(1002,'该订单已申请退款!');
                    }elseif($order->payment_status == 3){
                        return DRS::returnError(1002,'该订单已退款!');
                    }else{
                        return DRS::returnError(1002,'该订单支付状态出错!');
                    }
                }elseif ($order->status !== 1) {
                    return DRS::returnError(1002,'该订单状态不在支付状态!');
                }elseif ($order->delivery_id == null) {
                    return DRS::returnError(1002,'该订单未添加收货人地址!');
                }else{
                	$delivery = DB::table('delivery')
                        ->where([
                                    'id' 		=> $order->delivery_id, 
                                    'user_id' 	=> $userId, 
                                    'state' 	=> 0
                                ])
                        ->first();
                    if($delivery == null){
                    	return DRS::returnError(1003,'该收货地址不存在！');
                    }elseif($delivery->province == null || 
                    		$delivery->city == null || 
                    		$delivery->district == null ||
                    	    $delivery->address == null ||
                    		$delivery->phone == null ||
                    	    $delivery->name == null)
                    {
                    	return DRS::returnError(1003,'该收货地址信息不完整！');
                    }else{
                    	return DRS::returnOk((array)$unitOrderQuery);
                    }
                }
        }
	}

}